<%@ page import="News.service.Server" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="javax.swing.*" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/12/20 0020
  Time: 8:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主页</title>
</head>

<style>
    body{
        background-color: deepskyblue;
    }
    tr{
        text-align:center;
        width:1300px;
        border-bottom:1px dashed white;
        display:inline-block;
    }
    td{
        height: 40px;
    }
</style>
<body>
<div align="center">

    <form id="form" action="#" method="post">
        <h1 style="float: left;color: white">我的短信息</h1><br/><br/>
        <div align="right">当前用户：
            <span id="username">
                <%=
                    request.getParameter("username")
                %>
            </span>
            <span>发短消息</span>
            &nbsp;&nbsp;
            <a style="color: black;text-decoration: none" href="/App/Login.jsp">退出</a>
        </div><hr/>

        <table>
            <%
                String sql="SELECT `state`,`title`,`content`,`create_time`,`msgId`,`username`,`fromto`,`sendto` FROM WSQ.user_msg where `username`='"+request.getParameter("username")+"' and is_delete='1'";
                Server s=new Server();
                List<Map> list=s.selectUserMsg(sql);
                for(int i=0;i<list.size();i++){
                    out.print("<tr>");
                    if("0".equals(list.get(i).get("state"))){
                        out.print("<td class='state' style='float:left;font-size:19px'>未阅</td>");
                    }else{
                        out.print("<td class='state' style='float:left;font-size:19px'>已阅</td>");
                    }
                    out.print("<td class='title' style='float:left'>"+list.get(i).get("title")+"</td>");

                    if(list.get(i).get("content").toString().length()>10){
                        String arr[]=list.get(i).get("content").toString().split("",11);
                        out.print("<td class='content' style='float:left'>");
                        for(int j=0;j<10;j++){
                            out.print(arr[j]);
                        }
                        out.print("....");
                        out.print("</td>");
                    }else{
                        out.print("<td class='content' style='float:left'>"+list.get(i).get("content")+"</td>");
                    }

                    String arr[]=list.get(i).get("create_time").toString().split("",11);
                    out.print("<td style='float:right'>");
                    for(int j=0;j<10;j++){
                        out.print(arr[j]);
                    }
                    out.print("</td>");

                    out.print("<td class='insert' style='float:right'>回信</a></td>");
                    out.print("<td class='delete' style='float:right'>删除</td>");

                    out.print("<td class='msgId' style='display:none'>"+list.get(i).get("msgId")+"</td>");
                    out.print("<td class='username' style='display:none'>"+list.get(i).get("username")+"</td>");
                    out.print("<td class='fromto' style='display:none'>"+list.get(i).get("fromto")+"</td>");
                    out.print("<td class='sendto' style='display:none'>"+list.get(i).get("sendto")+"</td>");

                    out.print("</tr>");
                }

            %>
        </table>

    </form>
</div>
</body>
<script src="../../js/jquery-1.8.3.min.js"></script>
<script>
    function callBack(data){
        if(!data){
            alert("修改失败");
        }
        window.location.href=location.href;
    }
    function error(){
        alert("验证时，发生错误！");
    }
    $(function (){
        $(".state").click(function (){
            var msgId=$(this).siblings(".msgId").html();
            $.ajax({
                url:"/updateStateServlet",
                dataType:'text',
                type:"get",
                data:"msgId="+msgId,
                success:callBack,
                error:error
            })
        });
        $(".delete").click(function (){
            var msgId=$(this).next(".msgId").html();
            var username=$("#username").html().toString().trim();
            $.ajax({
                url:"/Servlet",
                dataType:'text',
                type:"get",
                data:"msgId="+msgId+"&username="+username,
                success:callBack,
                error:error
            })
        });
        $(".insert").click(function (){
            var title=window.prompt("请输入标题");
            var content=window.prompt("请输入内容");
            var fromto=$(this).siblings(".fromto").html().toString();
            var sendto=$(this).siblings(".sendto").html().toString();
            $.ajax({
                url:"/deleteServlet",
                dataType:'text',
                type:"get",
                data:"title="+title+"&content="+content+"&fromto="+fromto+"&sendto="+sendto,
                success:callBack,
                error:error
            })
        });
    });
</script>
</html>
